<template>
  <div class="body-container layout-padding">
    <div class="clearfix">
      <el-button size="default" type="primary"  @click="addStudent">
        <el-icon><ele-CirclePlus /></el-icon>添加
      </el-button>
    </div>
    <el-table :data="state.stuObj.list" border style="width: 100%">
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="年龄" prop="age"></el-table-column>
      <el-table-column label="生日">
        <template v-slot="scope">
          <DateFormat :value="scope.row.birth" :format="'YYYY-MM-DD'"></DateFormat>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="editStudent(scope.row.id)">修改</el-button>
          <el-button size="small" type="primary" @click="viewStudent(scope.row.id)">查看</el-button>
          <el-button size="small" type="danger"  @click="deleteStudent(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="mt15" background layout="total, prev, pager, next"
                   @current-change="loadStudent" @size-change="loadStudent" :total="state.stuObj.total"
                   v-model:current-page="state.sd.pageNum" v-model:page-size="state.sd.pageSize">
    </el-pagination>
    <el-dialog :title="state.popupTitle" v-model="state.entityDetailVisible" :close-on-click-modal="false" width="888px">
      <StudentDetail v-if="state.entityDetailVisible" v-model="state.entityDetailVisible" :entityId="state.popupEntityId"></StudentDetail>
    </el-dialog>
  </div>
</template>

<script setup name="studentIndex">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent} from "vue";
import { useRouter } from "vue-router";
const router = useRouter();

const StudentDetail = defineAsyncComponent(() => import('/@/views/basedata/student/studentDetail.vue'));
const state = reactive({
  sd: {
    pageNum: 1,
    pageSize: 10
  },
  stuObj: {},
  entityDetailVisible : false,
  popupTitle : '',
  popupEntityId : 0,
})
const loadStudent = ()=>{
  request({
    url: '/student/pagestudent',
    data: state.sd
  }).then(res => {
    const { data } = res
    state.stuObj = data
  })
}
const addStudent = ()=>{
  state.popupEntityId = Math.floor(Math.random() * 100000) * -1;
  state.popupTitle= "新增学生"
  state.entityDetailVisible= true
}
const editStudent = (id)=>{
  state.popupEntityId= id;
  state.popupTitle= "修改学生"
  state.entityDetailVisible= true
}
const viewStudent = (id)=>{
  router.push({name: 'basedataStudentView', params :{id : id} })
}
const deleteStudent = (id)=>{
  request({
    url: '/student/deletestudent',
    data: {id : id}
  }).then(res => {
    if(res.code === '200') {
      ElMessage.success(res.msg)
      loadStudent();
    }
  })
}

onMounted(()=>{
  loadStudent();
  mittBus.on("stuadd", ()=>{
    state.sd.pageNum = 1;
    loadStudent();
  })
  mittBus.on("stuedit", ()=>{
    loadStudent();
  })
})
onUnmounted(()=>{
  mittBus.off("stuadd", ()=>{})
  mittBus.off("stuedit",()=>{})
})

</script>
<style scoped>
</style>
